package com.catazy.ocrdemo.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialog
import com.catazy.mvvmlib.ext.view.click
import com.catazy.ocrdemo.databinding.DialogCommonBinding
import javax.inject.Inject

/**
 * 作者: Tao
 * 日期: 2024/2/23
 * 时间: 22:57
 * 描述: 等待对话框
 */
class CommonDialog(context: Context) : AppCompatDialog(context) {

    private var mBind: DialogCommonBinding

    private var mOnCommonListener: OnCommonListener? = null

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        mBind = DialogCommonBinding.inflate(layoutInflater, null, false)
        setContentView(mBind.root)

        // 设置背景透明
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val lp = window!!.attributes
        lp.dimAmount = 0.5f
        window?.attributes = lp

        val display = window?.windowManager?.defaultDisplay
        var width = 350
        display?.let {
            width = it.width * 9 / 10
        }
        window?.setLayout(
            width,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        window?.setGravity(Gravity.CENTER)

        mBind.commonCancel.click {
            dismiss()
        }

        mBind.commonSure.click {
            if (mOnCommonListener != null) {
                mOnCommonListener?.onSure()
            }
        }
    }

    /**
     * 设置标题
     * @param title 标题
     */
    fun setCTitle(title: String): CommonDialog {
        mBind.commonTitle.text = title
        return this
    }

    /**
     * 设置标题
     * @param resId 标题资源id
     */
    fun setCTitle(resId: Int): CommonDialog {
        mBind.commonTitle.text = context.getString(resId)
        return this
    }

    /**
     * 设置内容
     * @param msg 内容
     */
    fun setCMsg(msg: String): CommonDialog {
        mBind.commonMsg.text = msg
        return this
    }

    /**
     * 设置内容
     * @param resId 内容资源id
     */
    fun setCMsg(resId: Int): CommonDialog {
        mBind.commonMsg.text = context.getString(resId)
        return this
    }

    /**
     * 设置监听
     * @param listener 监听
     */
    fun setOnCommonListener(listener: OnCommonListener): CommonDialog {
        mOnCommonListener = listener
        return this
    }

    interface OnCommonListener {
        fun onSure()

        fun onCancel()
    }
}